package com.tybbank.Activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tybbank.Base.BaseActivity;
import com.tybbank.Bean.YZMBean;
import com.tybbank.Login.FindPasswordActivity;
import com.tybbank.Login.LoginActivity;
import com.tybbank.MyApplication;
import com.tybbank.R;
import com.tybbank.Utils.Constant;
import com.tybbank.Utils.MD5Utils;
import com.tybbank.Utils.MyDialogUtils;
import com.tybbank.Utils.YanZhengUtil;
import com.tybbank.Utils.logUtilssss;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.Comparator;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

/**
 * Created by Administrator on 2018/9/7 0007.
 */

public class UpdatePwdActivvity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.et_old_pwd)
    EditText etOldPwd;
    @BindView(R.id.et_new_pwd)
    EditText etNewPwd;
    @BindView(R.id.et_new_pwd_two)
    EditText etNewPwdTwo;
    @BindView(R.id.bt_enter)
    Button btEnter;
    @BindView(R.id.tv_forgetPsw)
    TextView tvForgetPsw;
    private String biaoji;
    private Intent intent;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_updatepwd);
        ButterKnife.bind(this); //登录密码
        toolbar(toolbar, getString(R.string.updatepwd), R.mipmap.icon_close);
    }

    @Override
    public void LeftClose() {
        super.LeftClose();
        UpdatePwdActivvity.this.finish();
    }

    @OnClick({R.id.tv_forgetPsw, R.id.bt_enter})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_forgetPsw: //忘记密码
                intent = new Intent(this, FindPasswordActivity.class);
                startActivity(intent);
                break;
            case R.id.bt_enter:
                if (TextUtils.isEmpty(etOldPwd.getText().toString().trim())) {
                    showTips(getString(R.string.oldpwd));
                } else if (TextUtils.isEmpty(etNewPwd.getText().toString().trim())) {
                    showTips(getString(R.string.newpwd));
                } else if (TextUtils.isEmpty(etNewPwdTwo.getText().toString().trim())) {
                    showTips(getString(R.string.newpwd));
                } else if (etNewPwdTwo.getText().toString().length() < 6 || etNewPwdTwo.getText().toString().length() > 32) {
                    showTips(getString(R.string.pwdhint));
                    return;
                } else if (!TextUtils.isEmpty(etNewPwdTwo.getText().toString().trim()) && (!etNewPwdTwo.getText().toString().trim().equals(etNewPwd.getText().toString().trim()))) {
                    Toast.makeText(getBaseContext(), getString(R.string.pwdnot), Toast.LENGTH_SHORT).show();
                } else {
                    if (YanZhengUtil.isLetterDigit(etNewPwd.getText().toString().trim()) == true) {
                        init();
                    } else {
                        Toast.makeText(getBaseContext(), getString(R.string.pwdhint), Toast.LENGTH_SHORT).show();
                        return;

                    }
                }
                break;
        }
    }

    private void init() {
        String time = MD5Utils.getTime();
        SortedMap<String, String> parameters = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });
        parameters.put("uuid", MyApplication.uuid);
        parameters.put("token", MyApplication.token);
        parameters.put("old_pwd", MD5Utils.md5Password(etOldPwd.getText().toString().trim()));
        parameters.put("new_pwd", MD5Utils.md5Password(etNewPwd.getText().toString().trim()));
        parameters.put("type", "1");
        parameters.put("TimeStamp", time);
        String sign = MD5Utils.createSign(parameters);
        initdata(time, sign);

    }

    private void initdata(String time, String sign) {
        final Dialog dialog = MyDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        dialog.show();
        OkHttpUtils
                .post()
                .url(Constant.modify_pwd)
                .addParams("uuid", MyApplication.uuid)
                .addParams("token", MyApplication.token)
                .addParams("type", "1")
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .addParams("old_pwd", MD5Utils.md5Password(etOldPwd.getText().toString().trim()))
                .addParams("new_pwd", MD5Utils.md5Password(etNewPwd.getText().toString().trim()))
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        Toast.makeText(getBaseContext(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        logUtilssss.e("TAG修改登录"+ response.toString());
                        json(response);
                        dialog.dismiss();

                    }

                    private void json(String json) {
                        try{
                            YZMBean meBean = new Gson().fromJson(json, new TypeToken<YZMBean>() {
                            }.getType());
                            int a = (meBean.getCode());
                            if (a == 1011) {
                                Toast.makeText(getBaseContext(), meBean.getMsg(), Toast.LENGTH_SHORT).show();
                                UpdatePwdActivvity.this.finish();
                            } else if (a == 1004) {
                                intent = new Intent(getBaseContext(), LoginActivity.class);
                                startActivity(intent);
                                return;
                            } else {
                                Toast.makeText(getBaseContext(), meBean.getMsg(), Toast.LENGTH_SHORT).show();
                            }
                        }catch (Exception e){

                        }
                    }
                });
    }
}
